﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Akso.Authorization;
using Akso.Classrooms.Dto;
using Akso.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;

namespace Akso.Classrooms
{
    //菜单权限
    [AbpAuthorize(AppPermissions.Pages_BasicDataModule_Classrooms)]
    public class ClassroomAppService: AbpZeroTemplateAppServiceBase, IClassroomAppService
    {
        private readonly IRepository<ClassroomsEntity, Guid> _classroomsRepository;
        
        public ClassroomAppService(IRepository<ClassroomsEntity, Guid> classroomsRepository)
        {
            _classroomsRepository = classroomsRepository;
           
        }
        private IQueryable<ClassroomsEntity> getQuery(GetClassroomsInput input)
        {
            var query = _classroomsRepository.GetAll()
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    t =>
                        t.ClassroomName.Contains(input.Filter) ||
                        t.Remark.Contains(input.Filter)
                );
            return query;
        }
        //获取教室信息
        public async Task<ListResultDto<ClassroomListDto>> GetClassrooms(GetClassroomsInput input)
        {
            var query = getQuery(input);
            var dataCount = await query.CountAsync();
            var data = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            return new PagedResultDto<ClassroomListDto>(
                dataCount,
                data.MapTo<List<ClassroomListDto>>()
                );
        }
        //删除教室信息
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_Classrooms_Delete)] //删除按钮权限
        public async Task DeleteClassroom(EntityDto<Guid> input)
        {
            var o = await _classroomsRepository.GetAsync(input.Id);
            await _classroomsRepository.DeleteAsync(o);
        }
        //根据ID判断是新增还是修改教室信息
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_Classrooms_Create,AppPermissions.Pages_BasicDataModule_Classrooms_Edit)]
        public async Task<ClassroomsEntity> CreateOrUpdateClassroom(CreateOrUpdateClassroomInput input)
        {
            //var vendor = input.Vendor.MapTo<Vendor>();
            //return await _vendorsRepository.InsertOrUpdateAsync(vendor);
            if (input.Classroom.Id.HasValue)
            {
                return await UpdateClassroom(input);
            }
            else
            {
                return await CreateClassroom(input);
            }
        }
        //修改教室
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_Classrooms_Edit)]
        protected virtual async Task<ClassroomsEntity> UpdateClassroom(CreateOrUpdateClassroomInput input)
        {
            Debug.Assert(input.Classroom.Id != null, "input.Classroom.Id should be set.");

            var o = await _classroomsRepository.GetAsync(input.Classroom.Id.Value);
            //Update classroom properties
            input.Classroom.MapTo(o);
            return await _classroomsRepository.UpdateAsync(o);
        }
        //新增教室
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_Classrooms_Create)]
        protected virtual async Task<ClassroomsEntity> CreateClassroom(CreateOrUpdateClassroomInput input)
        {
            var o = input.Classroom.MapTo<ClassroomsEntity>();
            return await _classroomsRepository.InsertAsync(o);
        }
        //根据ID获取编辑信息
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_Classrooms_Create, AppPermissions.Pages_BasicDataModule_Classrooms_Edit)]
        public async Task<GetClassroomForEditOutput> GetClassroomForEdit(NullableIdDto<Guid> input)
        {
            var output = new GetClassroomForEditOutput
            {

            };

            if (!input.Id.HasValue)
            {
                //Creating a new object
                output.Classroom = new ClassroomEditDto();
            }
            else
            {
                //Editing an existing object
                var vendor = await _classroomsRepository.GetAsync(input.Id.Value);

                output.Classroom = vendor.MapTo<ClassroomEditDto>();
            }

            return output;
        }
    }
}
